/****************************************************
	文件：GOMgr.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/10/10 15:51:6
	功能：GameObject 管理器（维护 GameObject 对象池）
*****************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using HTUtility;

namespace HTFramework.ResKit
{
    public class GOMgr : Singleton<GOMgr>
    {
        /// <summary>
        /// 对象池缓存（单一的 GameObject 的对象池）
        /// Key：资源路径
        /// </summary>
        private Dictionary<GOPoolType, GameObjectPool> mPoolDict;
        /// <summary>
        /// 场景中的回收节点（切换场景不销毁）
        /// </summary>
        private Transform mRecycleTrans;

        public void Init(Transform recycleTrans)
        {
            mPoolDict = new Dictionary<GOPoolType, GameObjectPool>();
            mRecycleTrans = recycleTrans;
            HTLogger.Info("GOMgr init done.");
        }

        /// <summary>
        /// 创建 GameObject 对象池
        /// </summary>
        /// <param name="path">资源路径</param>
        /// <param name="poolType">对象池类型</param>
        /// <param name="maxCount">对象池最大池容量</param>
        /// <param name="initialCount">对象池初始创建对象数量</param>
        /// <param name="createNewRecycleNode">是否创建新的回收节点</param>
        /// <param name="newRecycleNodeName">自定义回收节点名字</param>
        /// <returns></returns>
        public GameObjectPool CreateGameObjectPool(string path, GOPoolType poolType, int maxCount, int initialCount, bool createNewRecycleNode = false, string newRecycleNodeName = "")
        {
            if (mPoolDict.ContainsKey(poolType))
            {
                HTLogger.Error("重复创建 GameObject Pool，PoolType：{0}", poolType.ToString());
                return null;
            }
            Transform recycleTrans = null;
            if (createNewRecycleNode) //创建新的回收节点
            {
                string recycleName = "";
                if (newRecycleNodeName.Equals(""))
                {
                    recycleName = poolType.ToString();
                }
                else
                {
                    recycleName = newRecycleNodeName;
                }
                recycleTrans = new GameObject(recycleName).transform;
                recycleTrans.SetParent(mRecycleTrans);
            }
            else //使用默认的回收节点
            {
                recycleTrans = mRecycleTrans;
            }
            GameObjectPool pool = new GameObjectPool(path, recycleTrans, maxCount, initialCount);
            HTLogger.Debug("新建 GameObject Pool，poolType：{0}", poolType.ToString());
            mPoolDict.Add(poolType, pool);
            return pool;
        }
        /// <summary>
        /// 删除 GameObject 对象池
        /// </summary>
        /// <param name="poolType">对象池类型</param>
        /// <returns></returns>
        public bool DestroyGameObjectPool(GOPoolType poolType)
        {
            HTLogger.Debug("清除 GameObject Pool，poolType：{0}", poolType.ToString());
            GameObjectPool pool = GetGameObjectPool(poolType);
            if (pool == null)
            {
                HTLogger.Error("缓存中不存在此 GameObject Pool，poolType：{0}", poolType.ToString());
                return false;
            }
            pool.RecycleAll(false, false);
            mPoolDict.Remove(poolType);
            return true;
        }

        /// <summary>
        /// 获取已缓存的 GameObject 对象池
        /// </summary>
        /// <param name="poolType"></param>
        /// <returns></returns>
        public GameObjectPool GetGameObjectPool(GOPoolType poolType)
        {
            GameObjectPool pool = null;
            if (mPoolDict.TryGetValue(poolType, out pool))
            {
                HTLogger.Debug("从缓存中获取 GameObject Pool，poolType：{0}", poolType.ToString());
                return pool;
            }
            return null;
        }
    }
}
